package com.yuan.spring.io;

import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.FileCopyUtils;

import java.io.*;

/**
 * spring io 主要是对 文件的读取做了包装
 * fileCopyUtils 中包含对文件复制 读取 写出的相关操作
 * Created by 袁恩光 on 2017/6/12.
 */
public class ResourceLoader {

    @Test
    public void test() throws Exception {
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        //前缀 http:// ftp:// file: classpath:  没有前缀的 相对路径
        Resource resource = pathMatchingResourcePatternResolver.getResource("classpath:/static/test.txt");
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
        System.out.println(FileCopyUtils.copyToString(encodedResource.getReader()));
        //写出文件
        FileCopyUtils.copy("hello world".getBytes(), new File("D:/helloWorld.txt"));




        String path = "D:/helloWorld.txt";
        int dot = path.lastIndexOf(",");
        File file = new File(path.substring(0,dot));
        if(!file.exists()){
            file.mkdirs();
        }
        OutputStream outputStream = new FileOutputStream(path);
        FileCopyUtils.copy(resource.getInputStream(), outputStream);



    }
}
